//
//  CommunityMainHeadView.m
//  ArtDog
//
//  Created by dllo on 16/9/3.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "CommunityMainHeadView.h"
#import "BaseCollectionView.h"
#import "TopCollectionLayout.h"
#import "ShufView.h"
#import "Masonry.h"
#import "UIColor+Common.h"
#import "CommunityTarentoCollectionCell.h"

#import "CommunityAdvList.h"
#import "CommunityListUsers.h"

@interface CommunityMainHeadView ()<TopCollectionLayoutDelegate>

@property (nonatomic, strong) ShufView *shufView;

@property (nonatomic, strong) BaseCollectionView *collectionView;

@property (nonatomic, strong) UILabel *recommendLabel;

@property (nonatomic, strong) UIView *lineView;

@property (nonatomic, strong) UIView *bottomView;

@end

@implementation CommunityMainHeadView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setupViews];
        [self layoutAllViews];
        [self collectionViewProcotol];
    }
    
    return self;
}

- (void)setupViews
{
    self.backgroundColor = [UIColor whiteColor];
    
    _shufView = [[ShufView alloc]init];
    [self addSubview:_shufView];
    
    _recommendLabel = [[UILabel alloc]init];
    [self addSubview:_recommendLabel];
    _recommendLabel.text = @"推荐达人";
    _recommendLabel.textColor = [UIColor colorFromHexRGB:@"8a8a8a"];
    _recommendLabel.font = [UIFont systemFontOfSize:15];
    
    _lineView = [[UIView alloc]init];
    [self addSubview:_lineView];
    _lineView.backgroundColor = [UIColor colorFromHexRGB:@"f0f0f0"];
    
    TopCollectionLayout *layout = [[TopCollectionLayout alloc]init];
    _collectionView = [[BaseCollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
    [self addSubview:_collectionView];
    layout.minimumIteritemSpacing = 0;
    layout.sectionInset = UIEdgeInsetsMake(10, 0, 10, 0);
    layout.delegate = self;
    
    _collectionView.showsHorizontalScrollIndicator = NO;
    [_collectionView registerNib:[UINib nibWithNibName:@"CommunityTarentoCollectionCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"communityTarentoCollectionCell"];
    
    _bottomView = [[UIView alloc]init];
    [self addSubview:_bottomView];
    _bottomView.backgroundColor = [UIColor colorFromHexRGB:@"f0f0f0"];
    
}

- (void)layoutAllViews
{
    [_shufView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(self);
        make.height.equalTo(self.mas_width).multipliedBy(0.5);
    }];
    [_recommendLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(15);
        make.top.equalTo(_shufView.mas_bottom).offset(10);
    }];
    [_lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(15);
        make.top.equalTo(_recommendLabel.mas_bottom).offset(8);
        make.right.equalTo(self).offset(-15);
        make.height.mas_equalTo(1);
    }];
    [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self);
        make.bottom.equalTo(_bottomView.mas_top);
        make.top.equalTo(_lineView.mas_bottom);
    }];
    [_bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.equalTo(self);
        make.height.mas_equalTo(15);
    }];
}

#pragma mark - collectionView 协议方法
- (void)collectionViewProcotol
{
    [_collectionView collectionViewWithNumberOfItems:^(UICollectionView * _Nonnull collectionView, CollectionViewReturnParam * _Nonnull collectionViewParam) {
        collectionViewParam.numberOfItems = _listUsers.count;
    } andCellForItemAtIndexPath:^(UICollectionView * _Nonnull collectionView, NSIndexPath * _Nonnull indexPath, CollectionViewReturnParam * _Nonnull collectionViewParam) {
        CommunityTarentoCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"communityTarentoCollectionCell" forIndexPath:indexPath];
        cell.userModel = _listUsers[indexPath.item];
        collectionViewParam.returnCell = cell;
    } andDidSelectItemAtIndexPath:^(UICollectionView * _Nonnull collectionView, NSIndexPath * _Nonnull indexPath) {
        
    }];
}

#pragma mark - TopCollectionLayout delegate
- (CGSize)collectionView:(UICollectionView *)collectionView topLayout:(TopCollectionLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSString *username = _listUsers[indexPath.item].userName;
    NSString *viewer = [NSString stringWithFormat:@"%@人关注", _listUsers[indexPath.item].viewerNum];
    NSAttributedString *string;
    if (username.length > viewer.length) {
        string = [[NSAttributedString alloc]initWithString:username attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:15]}];
    } else {
        string = [[NSAttributedString alloc]initWithString:viewer attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:14]}];
    }
    
    CGRect newRect = [string boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, collectionView.frame.size.height - 20) options:NSStringDrawingUsesLineFragmentOrigin context:nil];
    return CGSizeMake(newRect.size.width + 90, collectionView.frame.size.height - 20);
    
}

#pragma mark - 数据
- (void)setAdvList:(NSArray<CommunityAdvList *> *)advList
{
    _advList = advList;
    NSMutableArray *images = [NSMutableArray array];
    for (CommunityAdvList *advModel in advList) {
        [images addObject:advModel.advPic];
    }
    _shufView.images = images;
    _shufView.timeInterval = 3;
}

- (void)setListUsers:(NSArray<CommunityListUsers *> *)listUsers
{
    _listUsers = listUsers;
}

@end
